<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ dataInfo.title }}</h1>

      <!-- 用户信息 -->
      <van-cell
        center
        :title="dataInfo.aut_name"
        :label="theTime(dataInfo.pubdate)"
      >
        <template #icon>
          <img :src="dataInfo.aut_photo" class="avatar" />
        </template>
        <template #default>
          <div>
            <van-button
              type="info"
              size="mini"
              v-if="dataInfo.is_followed"
              @click="followFn(true)"
              >已关注</van-button
            >
            <van-button
              icon="plus"
              type="info"
              size="mini"
              plain
              v-else
              @click="followFn(false)"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="dataInfo.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button
          icon="good-job"
          type="danger"
          size="small"
          v-if="dataInfo.attitude == 1"
          @click="isLike(-1)"
          >已点赞</van-button
        >
        <van-button
          icon="good-job-o"
          type="danger"
          plain
          size="small"
          v-else
          @click="isLike(1)"
          >点赞</van-button
        >
      </div>
    </div>
    <!-- 文章评论 -->
    <div>
      <CommentList> </CommentList>
    </div>
  </div>
</template>

<script>
import CommentList from "./CommentList";
import {
  reqArticleDetail,
  reqFollerAuthor,
  requnFollerAuthor,
  reqUserLike,
  reqUserDislike,
} from "@/api";
import { timeAgo } from "@/utils/day";
export default {
  name: "ArticleDetail",
  data() {
    return {
      dataInfo: {},
    };
  },
  components: {
    CommentList,
  },
  created() {
    this.getDetail();
  },
  methods: {
    async getDetail() {
      const res = await reqArticleDetail({
        art_id: this.$route.query.art_id,
      });
      if (res.status == 200) {
        this.dataInfo = res.data.data;
      }
    },
    theTime: timeAgo,
    async followFn(userSelect) {
      if (userSelect === true) {
        this.dataInfo.is_followed = false;
        await requnFollerAuthor({
          user_id: this.dataInfo.aut_id,
        });
      } else {
        this.dataInfo.is_followed = true;
        await reqFollerAuthor({
          user_id: this.dataInfo.aut_id,
        });
      }
    },
    async isLike(myType) {
      if (myType == -1) {
        this.dataInfo.attitude = -1;
        await reqUserDislike({
          art_id: this.dataInfo.art_id,
        });
      } else {
        this.dataInfo.attitude = 1;
        await reqUserLike({
          art_id: this.dataInfo.art_id,
        });
      }
    },
  },
};
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
